package cn.edu.swufe.surverpark.struts2.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.edu.swufe.surverpark.model.Role;
import cn.edu.swufe.surverpark.model.User;
import cn.edu.swufe.surverpark.struts2.service.RoleService;
import cn.edu.swufe.surverpark.struts2.service.UserService;

/**
 *@author 陈龙 E-mail:529914446@qq.com
 *@version创建时间:2015年5月6日下午10:40:57
 *类说明
 */
@Controller
@Scope("prototype")
public class UserAuthorizeAction extends BaseAction<User> {

	/**
	 * 
	 */
	@Resource
	private UserService userService;
	@Resource
	private RoleService roleService;
	
	private List<User> allUsers;
	private List<Role> noOwnRoles;
	private Integer[] ownRoleIds;


	
	private static final long serialVersionUID = 6638850378187377487L;

	public String findAllUsers() {
		this.allUsers = userService.findAllEntities();
		return "userAuthorizeListPage";
	}
	
	private Integer userId;
	public String editAuthorize(){
		this.model = userService.getEntityWithRoles(userId);
		this.noOwnRoles = roleService.findRolesNotInRange(model.getRoles());
		return "userAuthorizePage";
	}
	
	public String updateAuthorize(){
		userService.saveOrUpdateRole(model,ownRoleIds);
		return "findAllUsersAction";
	}
	
	public String clearAuthorize(){
		userService.clearAuthorize(userId);
		return "findAllUsersAction";
	}
	

	public List<User> getAllUsers() {
		return allUsers;
	}

	public void setAllUsers(List<User> allUsers) {
		this.allUsers = allUsers;
	}

	public List<Role> getNoOwnRoles() {
		return noOwnRoles;
	}

	public void setNoOwnRoles(List<Role> noOwnRoles) {
		this.noOwnRoles = noOwnRoles;
	}

	public Integer getUserId() {
		return userId;
	}


	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public Integer[] getOwnRoleIds() {
		return ownRoleIds;
	}

	public void setOwnRoleIds(Integer[] ownRoleIds) {
		this.ownRoleIds = ownRoleIds;
	}
	
}

